package com.ysd.lis.service.sta.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.sta.StaInstrGermDict;
import com.ysd.lis.entity.sta.StaGermDict;
import com.ysd.lis.mapper.sta.StaGermDictMapper;
import com.ysd.lis.request.StaGremDictDto;
import com.ysd.lis.request.Params;
import com.ysd.lis.request.AntibioticsDictParam;
import com.ysd.lis.service.sta.StaGermDictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 微生物基础字典 服务实现类
 * </p>
 *
 * @author HuaiXing
 * @since 2025-01-15
 */
@Service
public class StaGermDictServiceImpl extends ServiceImpl<StaGermDictMapper, StaGermDict> implements StaGermDictService {

    @Autowired
    StaGermDictMapper bacGermDictMapper;

    @Override
    public Result findGermList(Params params) {
        Page<StaGermDict> page = new Page<>();
        page.setCurrent(params.getPageIndex());
        page.setSize(params.getPageSize());

        LambdaQueryWrapper<StaGermDict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StaGermDict::getDelFlag, 0);
        if (!StringUtils.isEmpty(params.getSearchValue())) {
            queryWrapper.and(wrapper -> wrapper.like(StaGermDict::getGermNa, params.getSearchValue()).or().like(StaGermDict::getGermNo, params.getSearchValue()));
        }
        if (!BeanUtil.isEmpty(params.getGermType())) {
            queryWrapper.eq(StaGermDict::getGermType, params.getGermType());
        }
        if (!BeanUtil.isEmpty(params.getIsCommon()) && params.getIsCommon().equals(1)) {
            queryWrapper.eq(StaGermDict::getIsCommon, 1);
        }
        if (!BeanUtil.isEmpty(params.getIsCommon()) && params.getIsCommon().equals(0)) {
            queryWrapper.eq(StaGermDict::getIsCommon, 0);
        }
        Page<StaGermDict> bacGermDictPage = bacGermDictMapper.selectPage(page, queryWrapper);
        return Result.succ(1, "查询成功", bacGermDictPage);
    }

    @Override
    public Result getGermsByParams(Params params) {
        LambdaQueryWrapper<StaGermDict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StaGermDict::getDelFlag, 0);
        if (!StringUtils.isEmpty(params.getSearchValue())) {
            queryWrapper.and(wrapper -> wrapper.like(StaGermDict::getGermNa, params.getSearchValue()).or().like(StaGermDict::getGermNo, params.getSearchValue()));
        }
        if (!BeanUtil.isEmpty(params.getGermType())) {
            queryWrapper.eq(StaGermDict::getGermType, params.getGermType());
        }
        if (!BeanUtil.isEmpty(params.getIsCommon()) && params.getIsCommon().equals(1)) {
            queryWrapper.eq(StaGermDict::getIsCommon, 1);
        }
        if (!BeanUtil.isEmpty(params.getIsCommon()) && params.getIsCommon().equals(0)) {
            queryWrapper.eq(StaGermDict::getIsCommon, 0);
        }
        List<StaGermDict> bacGermDictPage = bacGermDictMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", bacGermDictPage);
    }

    @Override
    public Result addGermDict(StaGermDict bacGermDict) {

        int i = bacGermDictMapper.insert(bacGermDict);
        if (i > 0) {
            return Result.succ(1, "新增成功", "");
        } else {
            return Result.fail("添加微生物失败");
        }
    }

    @Override
    public Result updateGermDict(StaGermDict bacGermDict) {

        int i = bacGermDictMapper.updateById(bacGermDict);
        if (i > 0) {
            return Result.succ(1, "修改成功", "");
        } else {
            return Result.fail("修改微生物失败");
        }
    }

    @Override
    public Result batchDeleteGermDict(StaGremDictDto vo) {
        List<StaGermDict> bacGermDictList = vo.getBacGermDictList();

        List<String> ids = bacGermDictList.stream().map(StaGermDict::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }

    }

    @Override
    public Result findGermDictByInstrId(AntibioticsDictParam dictParam) {
        MPJLambdaWrapper<StaGermDict> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(StaGermDict.class).eq(StaGermDict::getDelFlag, 0);
        if (StrUtil.isNotEmpty(dictParam.getKeyWords())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(StaGermDict::getGermNo, dictParam.getKeyWords()).or().like(StaGermDict::getGermNa, dictParam.getKeyWords()));
        }
        if (StrUtil.isNotEmpty(dictParam.getInstrCode())) {
            StringBuffer existsSql = new StringBuffer("SELECT 1 FROM BAC_INSTR_GERM_DICT t2 WHERE t.GERM_NO = t2.GERM_NO AND t2.INSTR_CODE = '" + dictParam.getInstrCode() + "' ");
            lambdaQueryWrapper.notExists(existsSql.toString());
        }
        List<StaGermDict> bacGermDicts = bacGermDictMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功！", bacGermDicts);
    }

    @Override
    public Result findInstrGermDictsByInstrId(AntibioticsDictParam dictParam) {
        Assert.hasText(dictParam.getInstrCode(), "id must not be empty");
        MPJLambdaWrapper<StaGermDict> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .selectAll(StaGermDict.class)
                .selectAs(StaInstrGermDict::getId, StaGermDict::getInstrAntibDictId)
                .selectAs(StaInstrGermDict::getInputGermNo, StaGermDict::getInputGermNo)
                .selectAs(StaInstrGermDict::getInputGermNa, StaGermDict::getInputGermNa)
                .leftJoin(StaInstrGermDict.class, StaInstrGermDict::getGermNo, StaGermDict::getGermNo)
                .eq(StaInstrGermDict::getInstrCode, dictParam.getInstrCode()).eq(StaGermDict::getDelFlag, 0);
        if (StrUtil.isNotEmpty(dictParam.getKeyWords())) {
            mpjLambdaWrapper.and(wrapper -> wrapper.like(StaGermDict::getGermNo, dictParam.getKeyWords()).or().like(StaGermDict::getGermNa, dictParam.getKeyWords()));
        }
        List<StaGermDict> bacGermDicts = bacGermDictMapper.selectList(mpjLambdaWrapper);
        return Result.succ(1, "查询成功", bacGermDicts);
    }


}
